package com.apinji.alipay.service.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.apinji.alipay.common.AlipayErrorCodeEnum;
import com.apinji.alipay.dto.AlipayCloseDto;
import com.apinji.alipay.protocol.AlipayTradeCloseResponse;
import com.apinji.alipay.sdk.AlipaySdkConfig;
import com.apinji.alipay.service.AlipayCloseService;
import com.apinji.common.data.PayStatusEnum;
import com.apinji.common.domain.TransactionRecord;
import com.apinji.common.domain.mapper.TransactionRecordMapper;
import com.apinji.common.domain.wrapper.Reply;
import com.apinji.common.plugin.Mutual;
import com.apinji.common.plugin.StrUtils;
import com.apinji.common.plugin.fastjson.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlipayCloseServiceImpl implements AlipayCloseService {

    @Autowired
    private TransactionRecordMapper transactionRecordMapper;

    @Override
    public Reply close(AlipayCloseDto alipayCloseDto) throws Exception {
        TransactionRecord transactionRecord = transactionRecordMapper.getOneItemByUser(alipayCloseDto.getId(), alipayCloseDto.getUserUUID());
        if(null == transactionRecord || StrUtils.isEmpty(transactionRecord.getId())) {
            return Mutual.notExist("交易记录不存在，无法执行关闭交易操作");
        }
        //
        AlipaySdkConfig alipaySdkConfig = AlipaySdkConfig.getInstance();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(alipaySdkConfig.getGatewayUrl(),
                alipaySdkConfig.getAppId(),
                alipaySdkConfig.getMerchantPrivateKey(),
                "json", alipaySdkConfig.getCharset(),
                alipaySdkConfig.getAlipayPublicKey(),
                alipaySdkConfig.getSignType());
        //
        //设置请求参数
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = transactionRecord.getOutOrderId();
        //支付宝交易号
        String trade_no = transactionRecord.getQueryId();
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," +"\"trade_no\":\""+ trade_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        //

        JSONObject jsonObject = JSONUtils.parseJsonObject(result);
        AlipayTradeCloseResponse response = JSONUtils.toJavaObject(jsonObject.getJSONObject("alipay_trade_close_response"), AlipayTradeCloseResponse.class);
        if(null != response && response.getCode() == AlipayErrorCodeEnum.SUCCESS.getCode()) {
            transactionRecordMapper.updatePayStatus(transactionRecord.getId(), PayStatusEnum.CLOSED_DEAL.getStatus(), PayStatusEnum.CLOSED_DEAL.getName());
            return Mutual.success(response);
        }
        //
        return Mutual.alipayResponseError(response);
    }



}
